#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

//This Unit Test Will Test the Smithy Card.

//initialize game struct to array of 0's
void clearGameState(struct gameState *G) {
	int i;
	for (i=0; i<sizeof(struct gameState); i++) {
		((char *)G)[i] = 0;
	}
}

int main() {
	int i, j;
	int currentPlayer, handCount, deckCount;;	//relevant to refactored smithy card in dominion.c
	int seed = 1003;		//FEEL FREE TO CHANGE
	int unmatch = 0; 		//holds whether or not game states match when being compared
	int num_tests = 1000;		//FEEL FREE TO CHANGE
	int errors = 0;			//number of error'd tests
	
	//This test will run by initializing two game states, and comparing them by calling the function on one.
	struct gameState G, G2;
	
	//initialize random number using rngs.c library
	SelectStream(1);
	PutSeed((long)seed);
	
	printf("Now testing cardtest1 (Smithy card)\n");
	
	for (i=0; i<num_tests; i++) {
	  
	 //initialize two blank game states, (that match)
	 clearGameState(&G);
	 clearGameState(&G2);
	 unmatch = 0;
	 
	 //get random player number 1-4, and assign them to the appropriate turn in the game states.
	 currentPlayer = (int)(Random()*3)+1;
	 G.whoseTurn = G2.whoseTurn = currentPlayer;
	 
	 //randomize the number of actions, and assign them in the game states.
	 G.numActions = G2.numActions = (int)(Random()*2);
	 
	 //get random hand size and assign in both game states
	 handCount = (int)(Random()*MAX_HAND);
	 G.handCount[currentPlayer] = G2.handCount[currentPlayer] = handCount;
	 
	 //get random cards to fill these handcounts and assign in the game states
	 for (j=0; j<handCount; j++) {
	  G.hand[currentPlayer][j] = G2.hand[currentPlayer][j] = (int)(Random()*MAX_HAND); 
	 }
	 
	 //get random deck size and assign in both game states (and make sure it is at least >= 3)
	 deckCount = (int)((Random()*MAX_HAND)+3);
	 G.deckCount[currentPlayer] = G2.deckCount[currentPlayer] = deckCount;
	 
	 //get random cards to fill these deckcounts and assign in the game states
	 for (j=0; j<deckCount; j++) {
	  G.deck[currentPlayer][j] = G2.deck[currentPlayer][j] = (int)(Random()*MAX_HAND); 
	 }
	 
	 //put smithy card in each player's hand.
	 G.handCount[currentPlayer]++;
	 G2.handCount[currentPlayer]++;
	 G.hand[currentPlayer][handCount] = G2.hand[currentPlayer][handCount] = smithy;
	 
	 //Call smithy function on ONLY GameState 1.
	 cardEffect(smithy, 0, 0, 0, &G, handCount, NULL);
	 
	 // ALTERNATIVELY CALL THIS METHOD new_smithy(currentPlayer, handCount, &G);
	 
	 
	 //Now that G has had the Smithy card played, test ALL things done by the Smithy Card by comparing G and G2
	 //Test all three cards were added and one was discarded
	 if (G.handCount[currentPlayer] != (G2.handCount[currentPlayer]+2)) {
	   printf("Error, expected hand to have %d cards, instead it has %d cards\n", G2.handCount[currentPlayer]+2, G.handCount[currentPlayer]);
	   unmatch = 1;
	   errors++;
	 }
	 
	 //Test deck count was decremented
	 if (G.deckCount[currentPlayer] != (G2.deckCount[currentPlayer]-3)) {
	   printf("Error, expected deck to have %d cards, instead it has %d cards\n", G2.deckCount[currentPlayer]-3, G.deckCount[currentPlayer]);
	   unmatch = 1;
	   errors++;
	 }	 
	} //end main loop
	
	printf("Out of %d tests, %d returned error\n\n", num_tests, errors);
  
  return 0;
}

/*
//New Smithy Card Function
int new_smithy(int currentPlayer, int handPos, struct gameState *state) {
     int i;
	 
	 //+3 Cards
      for (i = 0; i < 3; i++) {
		drawCard(currentPlayer, state);
	 }
	 
      //discard card from hand
      discardCard(handPos, currentPlayer, state, 0);
      return 0;
}
*/